/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebas;

import Dominio.Entidades.Coordenada;
import Dominio.Entidades.Pieza;
import Dominio.Entidades.TipoPieza;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

/**
 *
 * @author d003341
 */
public class Checs extends JFrame {

    private JRadioButton radio1;
    private JRadioButton radio2;
    private JRadioButton radio3;
    private int tamanioBotones = 75;
    private int dimension = 4;
    private Color escalaGris = new Color(244, 242, 240);
    private Coordenada[] rotacionR = {new Coordenada(0, 0), new Coordenada(0, 1)};
    private Coordenada[] rotacionL = {new Coordenada(0, 0), new Coordenada(1, 0), new Coordenada(1, 1)};

    public Checs() {
        add(getGenero());
        add(getMasculino());
        add(getFemenino());
        add(getTito());
        getButtonGroup(getRadio1(), getRadio2(), getRadio3());
        add(this.radio1);
        add(this.radio2);
        add(this.radio3);
        dibujarCuadrado();
        //btnRotar.setBounds(150, 40, 60, 20);
//        add(this.btnRotar);
//        add(this.panelPieza);
        inicializador();
    }

    private void inicializador() {
        setSize(400, 300);
        //setLayout(null);
        setTitle("Aprendiendo Checkbox");
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JLabel getGenero() {
        JLabel l = new JLabel("Genero");
        l.setBounds(10, 80, 80, 30);
        return l;
    }

    private JLabel getMasculino() {
        JLabel l = new JLabel("Masculino");
        l.setBounds(10, 100, 80, 30);
        return l;
    }

    private JLabel getFemenino() {
        JLabel l = new JLabel("Femenino");
        l.setBounds(10, 115, 80, 30);
        return l;
    }

    private JLabel getTito() {
        JLabel l = new JLabel("Tiro");
        l.setBounds(10, 135, 80, 30);
        return l;
    }

    private JRadioButton getRadio1() {
        radio1 = new JRadioButton();
        radio1.setSelected(true);
        radio1.setBounds(80, 100, 20, 20);
        return radio1;
    }

    private JRadioButton getRadio2() {
        radio2 = new JRadioButton();
        radio2.setBounds(80, 120, 20, 20);
        return radio2;
    }

    private JRadioButton getRadio3() {
        radio3 = new JRadioButton();
        radio3.setBounds(80, 140, 20, 20);
        return radio3;
    }

    //Creación del grupo de botones recibe nuestro radiobutton creados
    //anteriormente
    private void getButtonGroup(JRadioButton radio1, JRadioButton radio2, JRadioButton radio3) {
        //creamos el objeto
        ButtonGroup bgroup = new ButtonGroup();
        //añadimos al grupo el primer radioButton
        bgroup.add(radio1);
        //añadimos al grupo el segundo radioButton
        bgroup.add(radio2);
        //añadimos al grupo el segundo radioButton
        bgroup.add(radio3);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        panelPieza = new javax.swing.JPanel();
        btnRotar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jRadioButton1.setText("jRadioButton1");

        jRadioButton2.setText("jRadioButton2");

        jRadioButton3.setText("jRadioButton3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton1))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton3)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelPiezaLayout = new javax.swing.GroupLayout(panelPieza);
        panelPieza.setLayout(panelPiezaLayout);
        panelPiezaLayout.setHorizontalGroup(
            panelPiezaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        panelPiezaLayout.setVerticalGroup(
            panelPiezaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        btnRotar.setText("jButton1");
        btnRotar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRotarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(200, 200, 200)
                .addComponent(btnRotar))
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(panelPieza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btnRotar)
                .addGap(17, 17, 17)
                .addComponent(panelPieza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRotarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRotarActionPerformed
        // TODO add your handling code here:
        rotarPieza();
    }//GEN-LAST:event_btnRotarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Checs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Checs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Checs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Checs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Checs().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRotar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JPanel panelPieza;
    // End of variables declaration//GEN-END:variables

    private void rotarPieza() {
        if (radio1.isSelected()) {
            dibujarCuadrado();
        }
        if (radio2.isSelected()) {
            rotarCoordenada(rotacionR[0]);
            rotarCoordenada(rotacionR[1]);
            dibujarRectangulo(2, 1);
        }
        if (radio3.isSelected()) {
            rotarCoordenada(rotacionL[0]);
            rotarCoordenada(rotacionL[1]);
            rotarCoordenada(rotacionL[2]);
            dibujarLadrilloL(2, 2);
        }
    }

    private void rotarCoordenada(Coordenada pCoordenada) {
        // TODO add your handling code here:
        if (pCoordenada.getPosFila() + pCoordenada.getPosColumna() == 0) {
            pCoordenada.setPosFila(0);
            pCoordenada.setPosColumna(1);
        } else if (pCoordenada.getPosFila() + pCoordenada.getPosColumna() == 2) {
            pCoordenada.setPosFila(1);
            pCoordenada.setPosColumna(0);
        } else {
            if (pCoordenada.getPosFila() == 0) {
                pCoordenada.setPosFila(1);
                pCoordenada.setPosColumna(1);
            }
            if (pCoordenada.getPosColumna() == 0) {
                pCoordenada.setPosFila(0);
                pCoordenada.setPosColumna(0);
            }
        }
    }

    private void dibujarCuadrado() {
        try {
            panelPieza.setLayout(new GridLayout(1, 1));
            panelPieza.removeAll();
            JButton jButton = new JButton();
            jButton.setSize(new Dimension(tamanioBotones, tamanioBotones));
//            jButton.addActionListener(new Partidas.ListenerBotonCuadrado(0, 0));
//            jButton.addMouseListener(new Partidas.ListenerBotonMouseCuadrado(0, 0));
//            botonesCuadrado = jButton;
            jButton.setBackground(Color.BLUE);
            //jButton.setEnabled(!finalizo);
            panelPieza.add(jButton);
            panelPieza.setBounds(panelPieza.getX(), panelPieza.getY(), tamanioBotones, tamanioBotones);
            panelPieza.updateUI();
        } catch (Exception ex) {
            //JOptionPane.ERROR_MESSAGE
            System.out.println(ex.getMessage());
        }
    }
    int nivel = 1;

    private void dibujarRectangulo(int pX, int pY) {
        try {
            panelPieza.setLayout(new GridLayout(pX, pY));
            panelPieza.removeAll();
            //panelPieza = new JButton[pX][pX];

            for (int posFila = 0; posFila < pX; posFila++) {
                for (int posColumna = 0; posColumna < pY; posColumna++) {
                    Pieza pieza = new Pieza(TipoPieza.Rectangulo, rotacionR);
                    if (pieza.contienePosicion(new Coordenada(posFila, posColumna))) {
                        JButton jButton = new JButton();
                        jButton.setSize(new Dimension(tamanioBotones, tamanioBotones));
//                        jButton.addActionListener(new Partidas.ListenerBotonRectangulo(posFila, posColumna));
//                        jButton.addMouseListener(new Partidas.ListenerBotonMouseRectangulo(posFila, posColumna));
                        panelPieza.add(jButton);
                        int decrementoColor = (3 + (3 * nivel));
                        nivel++;
//                        jButton.setBackground(new Color((escalaGris.getRed() - decrementoColor) % 255, (escalaGris.getGreen() - decrementoColor) % 255, (escalaGris.getBlue() - decrementoColor) % 255));
                        jButton.setBackground(Color.CYAN);
                        //escalaGris = new Color((escalaGris.getRed()-3)%255, (escalaGris.getGreen()-3)%255, (escalaGris.getBlue()-3)%255);

                        //jButton.setEnabled(!finalizo);
                        //botonesRectangulo[posFila][posColumna] = jButton;
                    } else {
                        //botones[posFila][posColumna].setBackground(new Color(107, 106, 104));
                        JLabel lbl = new JLabel(" ");

                        lbl.setSize(new Dimension(tamanioBotones, tamanioBotones));
                        panelPieza.add(lbl);
                    }
                }
            }
            panelPieza.setBounds(panelPieza.getX(), panelPieza.getY(), tamanioBotones * pX, tamanioBotones * pY);
            panelPieza.updateUI();
        } catch (Exception ex) {
            //JOptionPane.ERROR_MESSAGE
            System.out.println(ex.getMessage());
        }
    }

    private void dibujarLadrilloL(int pX, int pY) {
        try {
            panelPieza.setLayout(new GridLayout(pX, pY));
            panelPieza.removeAll();
            //panelPieza = new JButton[pX][pY];
            for (int posFila = 0; posFila < pX; posFila++) {
                for (int posColumna = 0; posColumna < pY; posColumna++) {
                    Pieza pieza = new Pieza(TipoPieza.LadrilloL, rotacionL);
                    //if (posFila == rotacionL.getPosFila() && posColumna == rotacionL.getPosColumna()) {
                    if (pieza.contienePosicion(new Coordenada(posFila, posColumna))) {
                        JButton jButton = new JButton();
                        jButton.setSize(new Dimension(tamanioBotones, tamanioBotones));
//                        jButton.addActionListener(new Partidas.ListenerBotonLadrilloL(posFila, posColumna));
//                        jButton.addMouseListener(new Partidas.ListenerBotonMouseLadrilloL(posFila, posColumna));
                        jButton.setBackground(Color.YELLOW);
                        //escalaGris = new Color((escalaGris.getRed()-3)%255, (escalaGris.getGreen()-3)%255, (escalaGris.getBlue()-3)%255);
                        //jButton.setEnabled(!finalizo);
                        panelPieza.add(jButton);
                        //panelPieza[posFila][posColumna] = jButton;
                    } else {
                        //botones[posFila][posColumna].setBackground(new Color(107, 106, 104));
                        Label lbl = new Label();
                        //lbl.setBackground(new Color(107, 106, 104));
                        panelPieza.add(lbl);
                    }
                }
            }
            panelPieza.setBounds(panelPieza.getX(), panelPieza.getY(), tamanioBotones * pX, tamanioBotones * pY);
            panelPieza.updateUI();
        } catch (Exception ex) {
            //JOptionPane.ERROR_MESSAGE
            System.out.println(ex.getMessage());
        }
    }
}
